﻿var app = angular.module('myAPP');
app.controller('HomeRouteController', function ($scope, $rootScope, $http, $location, $route, $modal, $sce, setLanguageFactory) {
    $scope.init = function () {
        $scope.activeHomeIcon = true;
        $scope.activeAboutIcon = false;
        $scope.activeProfileIcon = false;
        $scope.activeDictionaryIcon = false;
        $scope.activeQuestionIcon = false;
        $scope.activeContactIcon = false;
        $scope.activeSelHomeIcon = true;
        $scope.activeSelAboutIcon = false;
        $scope.activeSelProfileIcon = false;
        $scope.activeSelDictionaryIcon = false;
        $scope.activeSelQuestionIcon = false;
        $scope.activeSelContactIcon = false;
        $scope.showLogout = false;
        $scope.language = 'TH';
        $scope.headerName = '';
        $scope.checkExistLogin();
        $scope.setMainLanguage();
    };

    $scope.setLanguageThai = function () {
        $scope.language = 'TH';
        //$location.path();
        //console.log($location.path);
        $scope.setMainLanguage();
        $route.reload();
    };

    $scope.setLanguageEng = function () {
        $scope.language = 'EN';
        //$location.path();
        //console.log($location.path);
        $scope.setMainLanguage();
        $route.reload();
    };

    $scope.setMainLanguage = function () {
        $scope.lWelcome = setLanguageFactory.varWelcome($scope.language);
        $scope.lLogout = setLanguageFactory.varLogout($scope.language);
    };

    $scope.checkExistLogin = function () {
        $http({
            url: "../../PaydSw.asmx/CheckUserExistLogin",
            dataType: 'json',
            method: 'POST',
            data: '',
            headers: {
                "Content-Type": "application/json"
            }
        }).success(function (response) {
            var data = JSON.parse(response.d);

            if (data.Result != 0) {
                $scope.headerName = data.RecData.Title + " " + data.RecData.Name;
                $scope.showLogout = true;
            }
            else {
                $scope.headerName = "Guest";
                $scope.showLogout = false;

            }
        })
        .error(function (error) {

            var ErrorMessage = 'error at CheckUserExistLogin at HomeRouteController ' + error.ExceptionType + ' ' + error.Message + ' ' + error.StackTrace;
            alert(ErrorMessage);
            $scope.activeHomeIcon = true;
            $scope.activeSelHomeIcon = true;
            $location.path('/home');
        });
    }


    $scope.doLogout = function () {
        $http({
            url: "../../PaydSw.asmx/LogOutAction",
            dataType: 'json',
            method: 'POST',
            data: '',
            headers: {
                "Content-Type": "application/json"
            }
        }).success(function (response) {
            var data = JSON.parse(response.d);
            //console.log(response);
            if (data == 99) {
                alert("You have no permission in this webservice");
            }
            $scope.headerName = "Guest";
            $scope.showLogout = false;
            $scope.doselHome();
            $location.path('/home');
        })
        .error(function (error) {

            var ErrorMessage = 'error at LogOutAction at HomeRouteController ' + error.ExceptionType + ' ' + error.Message + ' ' + error.StackTrace;
            alert(ErrorMessage);
            $scope.headerName = "Guest";
            $scope.showLogout = false;
            $scope.doselHome();
            $location.path('/home');
        });
    };

    $scope.clearActive = function () {
        $scope.activeHomeIcon = false;
        $scope.activeAboutIcon = false;
        $scope.activeProfileIcon = false;
        $scope.activeDictionaryIcon = false;
        $scope.activeQuestionIcon = false;
        $scope.activeContactIcon = false;

    }

    $scope.clearSel = function () {
        $scope.activeSelHomeIcon = false;
        $scope.activeSelAboutIcon = false;
        $scope.activeSelProfileIcon = false;
        $scope.activeSelDictionaryIcon = false;
        $scope.activeSelQuestionIcon = false;
        $scope.activeSelContactIcon = false;
    }

    $scope.clearFlat = function () {
        $scope.clearActive();
        $scope.clearSel();
    }

    $scope.doselHome = function () {
        $scope.clearFlat();
        $scope.activeHomeIcon = true;
        $scope.activeSelHomeIcon = true;
    }

    $scope.doselAbout = function () {
        $scope.clearFlat();
        $scope.activeAboutIcon = true;
        $scope.activeSelAboutIcon = true;
    }

    $scope.doselProfile = function () {
        $scope.clearFlat();
        $scope.activeProfileIcon = true;
        $scope.activeSelProfileIcon = true;
    }

    $scope.doPermisionProfile = function () {
        $scope.doselProfile();
        $http({
            url: "../../PaydSw.asmx/CheckUserExistLogin",
            dataType: 'json',
            method: 'POST',
            data: '',
            headers: {
                "Content-Type": "application/json"
            }
        }).success(function (response) {
            var data = JSON.parse(response.d);

            if (data.Result != 0) {
                $location.path('/Profile');
            }
            else {
                $location.path('/login');
            }
        })
        .error(function (error) {

            var ErrorMessage = 'error at CheckCusExistLogin at HomeRouteController ' + error.ExceptionType + ' ' + error.Message + ' ' + error.StackTrace;
            alert(ErrorMessage);
            $scope.activeHomeIcon = true;
            $scope.activeSelHomeIcon = true;
            $location.path('/home');
        });

    }

    $scope.doselDictionary = function () {
        $scope.clearFlat();
        $scope.activeDictionaryIcon = true;
        $scope.activeSelDictionaryIcon = true;
    }

    $scope.doselQuestion = function () {
        $scope.clearFlat();
        $scope.activeQuestionIcon = true;
        $scope.activeSelQuestionIcon = true;
    }

    $scope.doselContact = function () {
        $scope.clearFlat();
        $scope.activeContactIcon = true;
        $scope.activeSelContactIcon = true;
    }

    $scope.doHomeHover = function () {
        if ($scope.activeHomeIcon != true) {

            $scope.activeHomeIcon = true;
        }
    };
    $scope.doHomeNotHover = function () {
        if ($scope.activeHomeIcon != false && $scope.activeSelHomeIcon == false) {
            $scope.activeHomeIcon = false;
        }
    };
    $scope.doAboutHover = function () {
        if ($scope.activeAboutIcon != true) {
            $scope.activeAboutIcon = true;
        }
    };
    $scope.doAboutNotHover = function () {
        if ($scope.activeAboutIcon != false && $scope.activeSelAboutIcon == false) {
            $scope.activeAboutIcon = false;
        }
    };
    $scope.doProfileHover = function () {
        if ($scope.activeProfileIcon != true) {
            $scope.activeProfileIcon = true;
        }
    };
    $scope.doProfileNotHover = function () {
        if ($scope.activeProfileIcon != false && $scope.activeSelProfileIcon == false) {
            $scope.activeProfileIcon = false;
        }
    };
    $scope.doDictionaryHover = function () {
        if ($scope.activeDictionaryIcon != true) {
            $scope.activeDictionaryIcon = true;
        }
    };
    $scope.doQuestionHover = function () {
        if ($scope.activeQuestionIcon != true) {
            $scope.activeQuestionIcon = true;
        }
    };
    $scope.doDictionaryNotHover = function () {
        if ($scope.activeDictionaryIcon != false && $scope.activeSelDictionaryIcon == false) {
            $scope.activeDictionaryIcon = false;
        }
    };
    $scope.doQuestionNotHover = function () {
        if ($scope.activeQuestionIcon != false && $scope.activeSelQuestionIcon == false) {
            $scope.activeQuestionIcon = false;
        }
    };
    $scope.doContactHover = function () {
        if ($scope.activeContactIcon != true) {
            $scope.activeContactIcon = true;
        }
    };
    $scope.doContactNotHover = function () {
        if ($scope.activeContactIcon != false && $scope.activeSelContactIcon == false) {
            $scope.activeContactIcon = false;
        }
    };

    $scope.searchAction = function () {
        $scope.searchValue = $scope.searchKeyword;
    };

    $scope.openAlertLogin = function (result, msgTitle, msgContent) {
        $scope.msg = {
            result: result,
            messageTitle: msgTitle,
            messageContent: msgContent
        }
        var modalInstance = $modal.open({
            templateUrl: '/UI/Modal/ModalAlert.aspx',
            //templateUrl: '/PaydSystem/UI/Modal/ModalAlert.aspx',
            controller: 'ModalAlertController',
            backdrop: true,
            keyboard: true,
            backdropClick: true,
            size: 'lg',
            resolve: {
                msg: function () {
                    return $scope.msg;
                }
            }
        });
        modalInstance.result.then(function (msg) {
            //console.log(msg);
            if (result == 1) {
                $scope.doselProfile();
                $location.path('/home');
            }

        }, function () {

        });

    };

    $scope.openAlert = function (result, msgTitle, msgContent) {
        $scope.msg = {
            result: result,
            messageTitle: msgTitle,
            messageContent: msgContent
        }
        var modalInstance = $modal.open({
            templateUrl: '/UI/Modal/ModalAlertWarn.aspx',
            //templateUrl: '/PaydSystem/UI/Modal/ModalAlertWarn.aspx',
            controller: 'ModalAlertWarnController',
            backdrop: true,
            keyboard: true,
            backdropClick: true,
            size: 'lg',
            resolve: {
                msg: function () {
                    return $scope.msg;
                }
            }
        });
        modalInstance.result.then(function () {
            //console.log(msg);


        }, function () {

        });

    };
})
.filter('highlight', function ($sce) {
    return function (text, phrase) {
        if (phrase) text = text.replace(new RegExp('(' + phrase + ')', 'gi'),
        '<span class="highlighted">$1</span>')

        return $sce.trustAsHtml(text)
    }
});